home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / DirectDraw / WindowedMode / windowedmode.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-31  |  18.4 KB  |  555 lines

  1. //-----------------------------------------------------------------------------
  2. // File: WindowedMode.cpp
  3. //
  4. // Desc: This sample demonstrates how to use DirectDraw in windowed mode.
  5. //
  6. // Copyright (c) 1999-2001 Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #define STRICT
  9. #include <windows.h>
  10. #include <ddraw.h>
  11. #include <mmsystem.h>
  12. #include "resource.h"
  13. #include "ddutil.h"
  14.  
  15.  
  16.  
  17.  
  18. //-----------------------------------------------------------------------------
  19. // Defines, constants, and global variables
  20. //-----------------------------------------------------------------------------
  21. #define SAFE_DELETE(p)  { if(p) { delete (p);     (p)=NULL; } }
  22. #define SAFE_RELEASE(p) { if(p) { (p)->Release(); (p)=NULL; } }
  23.  
  24. #define WINDOW_WIDTH    640
  25. #define WINDOW_HEIGHT   480
  26.  
  27. #define SPRITE_DIAMETER 48
  28. #define NUM_SPRITES     25
  29.  
  30. #define HELPTEXT TEXT("Press Escape to quit.")
  31.  
  32. struct SPRITE_STRUCT
  33. {
  34.     FLOAT fPosX; 
  35.     FLOAT fPosY;
  36.     FLOAT fVelX; 
  37.     FLOAT fVelY;
  38. };
  39.  
  40. CDisplay*            g_pDisplay        = NULL;
  41. CSurface*            g_pLogoSurface    = NULL;  
  42. CSurface*            g_pTextSurface    = NULL;  
  43. RECT                 g_rcViewport;          
  44. RECT                 g_rcScreen;            
  45. BOOL                 g_bActive     = FALSE; 
  46. DWORD                g_dwLastTick;
  47. SPRITE_STRUCT        g_Sprite[NUM_SPRITES]; 
  48.  
  49.  
  50.  
  51.  
  52. //-----------------------------------------------------------------------------
  53. // Function-prototypes
  54. //-----------------------------------------------------------------------------
  55. LRESULT CALLBACK MainWndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam );
  56. HRESULT WinInit( HINSTANCE hInst, int nCmdShow, HWND* phWnd, HACCEL* phAccel );
  57. HRESULT InitDirectDraw( HWND hWnd );
  58. VOID    FreeDirectDraw();
  59. HRESULT ProcessNextFrame( HWND hWnd );
  60. VOID    UpdateSprite( SPRITE_STRUCT* pSprite, FLOAT fTimeDelta );
  61. HRESULT DisplayFrame();
  62. HRESULT RestoreSurfaces();
  63.  
  64.  
  65.  
  66.  
  67. //-----------------------------------------------------------------------------
  68. // Name: WinMain()
  69. // Desc: Entry point to the program. Initializes everything and calls
  70. //       UpdateFrame() when idle from the message pump.
  71. //-----------------------------------------------------------------------------
  72. int APIENTRY WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR pCmdLine, int nCmdShow )
  73. {
  74.     MSG         msg;
  75.     HWND     hWnd;
  76.     HACCEL   hAccel;
  77.  
  78.     ZeroMemory( &g_Sprite, sizeof(SPRITE_STRUCT) * NUM_SPRITES );
  79.     srand( GetTickCount() );
  80.  
  81.     if( FAILED( WinInit( hInst, nCmdShow, &hWnd, &hAccel ) ) )
  82.         return FALSE;
  83.  
  84.     if( FAILED( InitDirectDraw( hWnd ) ) )
  85.     {
  86.         MessageBox( hWnd, TEXT("DirectDraw init failed. ")
  87.                     TEXT("The sample will now exit. "), TEXT("DirectDraw Sample"), 
  88.                     MB_ICONERROR | MB_OK );
  89.         return FALSE;
  90.     }
  91.  
  92.     g_dwLastTick = timeGetTime();
  93.  
  94.     while( TRUE )
  95.     {
  96.         // Look for messages, if none are found then 
  97.         // update the state and display it
  98.         if( PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE ) )
  99.         {
  100.             if( 0 == GetMessage(&msg, NULL, 0, 0 ) )
  101.             {
  102.                 // WM_QUIT was posted, so exit
  103.                 return (int)msg.wParam;
  104.             }
  105.  
  106.             // Translate and dispatch the message
  107.             if( 0 == TranslateAccelerator( hWnd, hAccel, &msg ) )
  108.             {
  109.                 TranslateMessage( &msg ); 
  110.                 DispatchMessage( &msg );
  111.             }
  112.         }
  113.         else
  114.         {
  115.             if( g_bActive )
  116.             {
  117.                 // Move the sprites, blt them to the back buffer, then 
  118.                 // flip or blt the back buffer to the primary buffer
  119.                 if( FAILED( ProcessNextFrame( hWnd ) ) )
  120.                 {
  121.                     SAFE_DELETE( g_pDisplay );
  122.  
  123.                     MessageBox( hWnd, TEXT("Displaying the next frame failed. ")
  124.                                 TEXT("The sample will now exit. "), TEXT("DirectDraw Sample"), 
  125.                                 MB_ICONERROR | MB_OK );
  126.                     return FALSE;
  127.                 }
  128.             }
  129.             else
  130.             {
  131.                 // Make sure we go to sleep if we have nothing else to do
  132.                 WaitMessage();
  133.  
  134.                 // Ignore time spent inactive 
  135.                 g_dwLastTick = timeGetTime();
  136.             }
  137.         }
  138.     }
  139. }
  140.  
  141.  
  142.  
  143.  
  144. //-----------------------------------------------------------------------------
  145. // Name: WinInit()
  146. // Desc: Init the window
  147. //-----------------------------------------------------------------------------
  148. HRESULT WinInit( HINSTANCE hInst, int nCmdShow, HWND* phWnd, HACCEL* phAccel )
  149. {
  150.     WNDCLASSEX wc;
  151.     HWND       hWnd;
  152.     HACCEL     hAccel;
  153.  
  154.     // Register the Window Class
  155.     wc.cbSize        = sizeof(wc);
  156.     wc.lpszClassName = TEXT("WindowedMode");
  157.     wc.lpfnWndProc   = MainWndProc;
  158.     wc.style         = CS_VREDRAW | CS_HREDRAW;
  159.     wc.hInstance     = hInst;
  160.     wc.hIcon         = LoadIcon( hInst, MAKEINTRESOURCE(IDI_MAIN) );
  161.     wc.hIconSm       = LoadIcon( hInst, MAKEINTRESOURCE(IDI_MAIN) );
  162.     wc.hCursor       = LoadCursor( NULL, IDC_ARROW );
  163.     wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
  164.     wc.lpszMenuName  = MAKEINTRESOURCE(IDR_MENU);
  165.     wc.cbClsExtra    = 0;
  166.     wc.cbWndExtra    = 0;
  167.  
  168.     if( RegisterClassEx( &wc ) == 0 )
  169.         return E_FAIL;
  170.  
  171.     // Load keyboard accelerators
  172.     hAccel = LoadAccelerators( hInst, MAKEINTRESOURCE(IDR_MAIN_ACCEL) );
  173.  
  174.     // Calculate the proper size for the window given a client of 640x480
  175.     DWORD dwFrameWidth    = GetSystemMetrics( SM_CXSIZEFRAME );
  176.     DWORD dwFrameHeight   = GetSystemMetrics( SM_CYSIZEFRAME );
  177.     DWORD dwMenuHeight    = GetSystemMetrics( SM_CYMENU );
  178.     DWORD dwCaptionHeight = GetSystemMetrics( SM_CYCAPTION );
  179.     DWORD dwWindowWidth   = WINDOW_WIDTH  + dwFrameWidth * 2;
  180.     DWORD dwWindowHeight  = WINDOW_HEIGHT + dwFrameHeight * 2 + 
  181.                             dwMenuHeight + dwCaptionHeight;
  182.  
  183.     // Create and show the main window
  184.     DWORD dwStyle = WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX;
  185.     hWnd = CreateWindowEx( 0, TEXT("WindowedMode"), TEXT("DirectDraw WindowedMode Sample"),
  186.                            dwStyle, CW_USEDEFAULT, CW_USEDEFAULT,
  187.                              dwWindowWidth, dwWindowHeight, NULL, NULL, hInst, NULL );
  188.     if( hWnd == NULL )
  189.         return E_FAIL;
  190.  
  191.     ShowWindow( hWnd, nCmdShow );
  192.     UpdateWindow( hWnd );
  193.  
  194.     *phWnd   = hWnd;
  195.     *phAccel = hAccel;
  196.  
  197.     return S_OK;
  198. }
  199.  
  200.  
  201.  
  202.  
  203. //-----------------------------------------------------------------------------
  204. // Name: InitDirectDraw()
  205. // Desc: Create the DirectDraw object, and init the surfaces
  206. //-----------------------------------------------------------------------------
  207. HRESULT InitDirectDraw( HWND hWnd )
  208. {
  209.     LPDIRECTDRAWPALETTE pDDPal = NULL; 
  210.     HRESULT    hr;
  211.     int     iSprite;
  212.  
  213.     g_pDisplay = new CDisplay();
  214.     if( FAILED( hr = g_pDisplay->CreateWindowedDisplay( hWnd, WINDOW_WIDTH, WINDOW_HEIGHT ) ) )
  215.     {
  216.         MessageBox( hWnd, TEXT("Failed initializing DirectDraw."),
  217.                     TEXT("DirectDraw Sample"), MB_ICONERROR | MB_OK );
  218.         return hr;
  219.     }
  220.  
  221.     // Create and set the palette when in palettized color
  222.     if( FAILED( hr = g_pDisplay->CreatePaletteFromBitmap( &pDDPal, MAKEINTRESOURCE( IDB_DIRECTX ) ) ) )
  223.         return hr;
  224.  
  225.     g_pDisplay->SetPalette( pDDPal );
  226.  
  227.     SAFE_RELEASE( pDDPal );
  228.  
  229.     // Create a surface, and draw a bitmap resource on it.  
  230.     if( FAILED( hr = g_pDisplay->CreateSurfaceFromBitmap( &g_pLogoSurface, MAKEINTRESOURCE( IDB_DIRECTX ), 
  231.                                                           SPRITE_DIAMETER, SPRITE_DIAMETER ) ) )
  232.         return hr;
  233.  
  234.     // Create a surface, and draw text to it.  
  235.     if( FAILED( hr = g_pDisplay->CreateSurfaceFromText( &g_pTextSurface, NULL, HELPTEXT, 
  236.                                                         RGB(0,0,0), RGB(255, 255, 0) ) ) )
  237.         return hr;
  238.  
  239.     // Set the color key for the logo sprite to black
  240.     if( FAILED( hr = g_pLogoSurface->SetColorKey( 0 ) ) )
  241.         return hr;
  242.  
  243.     // Init all the sprites.  All of these sprites look the same, 
  244.     // using the g_pDDSLogo surface. 
  245.     for( iSprite = 0; iSprite < NUM_SPRITES; iSprite++ )
  246.     {
  247.         // Set the sprite's position and velocity
  248.         g_Sprite[iSprite].fPosX = (float) (rand() % WINDOW_WIDTH);
  249.         g_Sprite[iSprite].fPosY = (float) (rand() % WINDOW_HEIGHT); 
  250.  
  251.         g_Sprite[iSprite].fVelX = 500.0f * rand() / RAND_MAX - 250.0f;
  252.         g_Sprite[iSprite].fVelY = 500.0f * rand() / RAND_MAX - 250.0f;
  253.     }
  254.  
  255.     return S_OK;
  256. }
  257.  
  258.  
  259.  
  260.  
  261. //-----------------------------------------------------------------------------
  262. // Name: FreeDirectDraw()
  263. // Desc: Release all the DirectDraw objects
  264. //-----------------------------------------------------------------------------
  265. VOID FreeDirectDraw()
  266. {
  267.     SAFE_DELETE( g_pLogoSurface );
  268.     SAFE_DELETE( g_pTextSurface );
  269.     SAFE_DELETE( g_pDisplay );
  270. }
  271.  
  272.  
  273.  
  274.  
  275. //-----------------------------------------------------------------------------
  276. // Name: MainWndProc()
  277. // Desc: The main window procedure
  278. //-----------------------------------------------------------------------------
  279. LRESULT CALLBACK MainWndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
  280. {
  281.     switch (msg)
  282.     {
  283.         case WM_COMMAND:
  284.             switch( LOWORD(wParam) )
  285.             {
  286.                 case IDM_EXIT:
  287.                     // Received key/menu command to exit app
  288.                     PostMessage( hWnd, WM_CLOSE, 0, 0 );
  289.                     return 0L;
  290.             }
  291.             break; // Continue with default processing
  292.  
  293.         case WM_PAINT:
  294.             // Update the screen if we need to refresh. This case occurs 
  295.             // when in windowed mode and the window is behind others.
  296.             // The app will not be active, but it will be visible.
  297.             if( g_pDisplay )
  298.             {
  299.                 // Display the new position of the sprite
  300.                 if( DisplayFrame() == DDERR_SURFACELOST )
  301.                 {
  302.                     // If the surfaces were lost, then restore and try again
  303.                     RestoreSurfaces();
  304.                     DisplayFrame();
  305.                 }
  306.             }
  307.             break; // Continue with default processing to validate the region
  308.  
  309.         case WM_QUERYNEWPALETTE:
  310.             if( g_pDisplay )
  311.             {
  312.                 // If we are in windowed mode with a desktop resolution in 8 bit 
  313.                 // color, then the palette we created during init has changed 
  314.                 // since then.  So get the palette back from the primary 
  315.                 // DirectDraw surface, and set it again so that DirectDraw 
  316.                 // realises the palette, then release it again. 
  317.                 LPDIRECTDRAWPALETTE pDDPal = NULL; 
  318.                 g_pDisplay->GetFrontBuffer()->GetPalette( &pDDPal );
  319.                 g_pDisplay->GetFrontBuffer()->SetPalette( pDDPal );
  320.                 SAFE_RELEASE( pDDPal );
  321.             }
  322.             break;
  323.  
  324.         case WM_GETMINMAXINFO:
  325.             {
  326.                 // Don't allow resizing in windowed mode.  
  327.                 // Fix the size of the window to 640x480 (client size)
  328.                 MINMAXINFO* pMinMax = (MINMAXINFO*) lParam;
  329.  
  330.                 DWORD dwFrameWidth    = GetSystemMetrics( SM_CXSIZEFRAME );
  331.                 DWORD dwFrameHeight   = GetSystemMetrics( SM_CYSIZEFRAME );
  332.                 DWORD dwMenuHeight    = GetSystemMetrics( SM_CYMENU );
  333.                 DWORD dwCaptionHeight = GetSystemMetrics( SM_CYCAPTION );
  334.  
  335.                 pMinMax->ptMinTrackSize.x = WINDOW_WIDTH  + dwFrameWidth * 2;
  336.                 pMinMax->ptMinTrackSize.y = WINDOW_HEIGHT + dwFrameHeight * 2 + 
  337.                                             dwMenuHeight + dwCaptionHeight;
  338.  
  339.                 pMinMax->ptMaxTrackSize.x = pMinMax->ptMinTrackSize.x;
  340.                 pMinMax->ptMaxTrackSize.y = pMinMax->ptMinTrackSize.y;
  341.             }
  342.             return 0L;
  343.  
  344.         case WM_MOVE:
  345.             if( g_pDisplay )
  346.                 g_pDisplay->UpdateBounds();
  347.             return 0L;
  348.  
  349.         case WM_EXITMENULOOP:
  350.             // Ignore time spent in menu
  351.             g_dwLastTick = timeGetTime();
  352.             break;
  353.  
  354.         case WM_EXITSIZEMOVE:
  355.             // Ignore time spent resizing
  356.             g_dwLastTick = timeGetTime();
  357.             break;
  358.  
  359.         case WM_SIZE:
  360.             // Check to see if we are losing our window...
  361.             if( SIZE_MAXHIDE==wParam || SIZE_MINIMIZED==wParam )
  362.                 g_bActive = FALSE;
  363.             else
  364.                 g_bActive = TRUE;
  365.  
  366.             if( g_pDisplay )
  367.                 g_pDisplay->UpdateBounds();
  368.             break;
  369.             
  370.         case WM_DESTROY:
  371.             // Cleanup and close the app
  372.             FreeDirectDraw();
  373.             PostQuitMessage( 0 );
  374.             return 0L;
  375.     }
  376.  
  377.     return DefWindowProc(hWnd, msg, wParam, lParam);
  378. }
  379.  
  380.  
  381.  
  382.  
  383. //-----------------------------------------------------------------------------
  384. // Name: ProcessNextFrame()
  385. // Desc: Move the sprites, blt them to the back buffer, then 
  386. //       flip or blt the back buffer to the primary buffer
  387. //-----------------------------------------------------------------------------
  388. HRESULT ProcessNextFrame( HWND hWnd )
  389. {
  390.     HRESULT hr;
  391.  
  392.     // Figure how much time has passed since the last time
  393.     DWORD dwCurrTick = timeGetTime();
  394.     DWORD dwTickDiff = dwCurrTick - g_dwLastTick;
  395.  
  396.     // Don't update if no time has passed 
  397.     if( dwTickDiff == 0 )
  398.         return S_OK; 
  399.  
  400.     g_dwLastTick = dwCurrTick;
  401.  
  402.     // Move the sprites according to how much time has passed
  403.     for( int iSprite = 0; iSprite < NUM_SPRITES; iSprite++ )
  404.         UpdateSprite( &g_Sprite[ iSprite ], dwTickDiff / 1000.0f );
  405.  
  406.     // Check the cooperative level before rendering
  407.     if( FAILED( hr = g_pDisplay->GetDirectDraw()->TestCooperativeLevel() ) )
  408.     {
  409.         switch( hr )
  410.         {
  411.             case DDERR_EXCLUSIVEMODEALREADYSET:
  412.                 // Do nothing because some other app has exclusive mode
  413.                 Sleep(10);
  414.                 return S_OK;
  415.  
  416.             case DDERR_WRONGMODE:
  417.                 // The display mode changed on us. Update the
  418.                 // DirectDraw surfaces accordingly
  419.                 FreeDirectDraw();
  420.                 return InitDirectDraw( hWnd );
  421.         }
  422.         return hr;
  423.     }
  424.  
  425.     // Display the sprites on the screen
  426.     if( FAILED( hr = DisplayFrame() ) )
  427.     {
  428.         if( hr != DDERR_SURFACELOST )
  429.             return hr;
  430.  
  431.         // The surfaces were lost so restore them 
  432.         RestoreSurfaces();
  433.     }
  434.  
  435.     return S_OK;
  436. }
  437.  
  438.  
  439.  
  440.  
  441. //-----------------------------------------------------------------------------
  442. // Name: UpdateSprite()
  443. // Desc: Move the sprite around and make it bounce based on how much time 
  444. //       has passed
  445. //-----------------------------------------------------------------------------
  446. VOID UpdateSprite( SPRITE_STRUCT* pSprite, FLOAT fTimeDelta )
  447. {    
  448.     // Update the sprite position
  449.     pSprite->fPosX += pSprite->fVelX * fTimeDelta;
  450.     pSprite->fPosY += pSprite->fVelY * fTimeDelta;
  451.  
  452.     // Clip the position, and bounce if it hits the edge
  453.     if( pSprite->fPosX < 0.0f )
  454.     {
  455.         pSprite->fPosX  = 0;
  456.         pSprite->fVelX = -pSprite->fVelX;
  457.     }
  458.  
  459.     if( pSprite->fPosX >= WINDOW_WIDTH - SPRITE_DIAMETER )
  460.     {
  461.         pSprite->fPosX = WINDOW_WIDTH - 1 - SPRITE_DIAMETER;
  462.         pSprite->fVelX = -pSprite->fVelX;
  463.     }
  464.  
  465.     if( pSprite->fPosY < 0 )
  466.     {
  467.         pSprite->fPosY = 0;
  468.         pSprite->fVelY = -pSprite->fVelY;
  469.     }
  470.  
  471.     if( pSprite->fPosY > WINDOW_HEIGHT - SPRITE_DIAMETER )
  472.     {
  473.         pSprite->fPosY = WINDOW_HEIGHT - 1 - SPRITE_DIAMETER;
  474.         pSprite->fVelY = -pSprite->fVelY;
  475.     }   
  476. }
  477.  
  478.  
  479.  
  480.  
  481. //-----------------------------------------------------------------------------
  482. // Name: DisplayFrame()
  483. // Desc: Blts a the sprites to the back buffer, then it blts or flips the 
  484. //       back buffer onto the primary buffer.
  485. //-----------------------------------------------------------------------------
  486. HRESULT DisplayFrame()
  487. {
  488.     HRESULT hr;
  489.  
  490.     // Fill the back buffer with black, ignoring errors until the flip
  491.     g_pDisplay->Clear( 0 );
  492.  
  493.     // Blt the help text on the backbuffer, ignoring errors until the flip
  494.     g_pDisplay->Blt( 10, 10, g_pTextSurface, NULL );
  495.  
  496.     // Blt all the sprites onto the back buffer using color keying,
  497.     // ignoring errors until the last blt. Note that all of these sprites 
  498.     // use the same DirectDraw surface.
  499.     for( int iSprite = 0; iSprite < NUM_SPRITES; iSprite++ )
  500.     {
  501.         g_pDisplay->Blt( (DWORD)g_Sprite[iSprite].fPosX, 
  502.                          (DWORD)g_Sprite[iSprite].fPosY, 
  503.                          g_pLogoSurface, NULL );
  504.     }
  505.  
  506.     // We are in windowed mode so perform a blt from the backbuffer 
  507.     // to the primary, returning any errors like DDERR_SURFACELOST
  508.     if( FAILED( hr = g_pDisplay->Present() ) )
  509.         return hr;
  510.  
  511.     return S_OK;
  512. }
  513.  
  514.  
  515.  
  516.  
  517. //-----------------------------------------------------------------------------
  518. // Name: RestoreSurfaces()
  519. // Desc: Restore all the surfaces, and redraw the sprite surfaces.
  520. //-----------------------------------------------------------------------------
  521. HRESULT RestoreSurfaces()
  522. {
  523.     LPDIRECTDRAWPALETTE pDDPal = NULL; 
  524.     HRESULT hr;
  525.  
  526.     if( FAILED( hr = g_pDisplay->GetDirectDraw()->RestoreAllSurfaces() ) )
  527.         return hr;
  528.  
  529.     // No need to re-create the surface, just re-draw it.
  530.     if( FAILED( hr = g_pTextSurface->DrawText( NULL, HELPTEXT, 
  531.                                                0, 0, RGB(0,0,0), RGB(255, 255, 0) ) ) )
  532.         return hr;
  533.  
  534.     // We need to release and re-load, and set the palette again to 
  535.     // redraw the bitmap on the surface.  Otherwise, GDI will not 
  536.     // draw the bitmap on the surface with the right palette
  537.     if( FAILED( hr = g_pDisplay->CreatePaletteFromBitmap( &pDDPal, MAKEINTRESOURCE( IDB_DIRECTX ) ) ) )
  538.         return hr;
  539.  
  540.     g_pDisplay->SetPalette( pDDPal );
  541.  
  542.     SAFE_RELEASE( pDDPal );
  543.  
  544.     // No need to re-create the surface, just re-draw it.
  545.     if( FAILED( hr = g_pLogoSurface->DrawBitmap( MAKEINTRESOURCE( IDB_DIRECTX ),
  546.                                                  SPRITE_DIAMETER, SPRITE_DIAMETER ) ) )
  547.         return hr;
  548.  
  549.     return S_OK;
  550. }
  551.  
  552.  
  553.  
  554.  
  555.